package com.winit.openapi.ups.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.gt.GlobalTransferService;
import com.winit.oms.spi.gt.command.GlobalTransferImportDeclareRuleCommand;
import com.winit.oms.spi.inbound.InboundOrderService;
import com.winit.oms.spi.inbound.command.InboundOrderImportDeclareRuleCommand;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.ups.spi.customstrade.ImportDeclarationRulesService;
import com.winit.ups.spi.customstrade.command.QueryImportDeclarationRulesByCodeCommand;
import com.winit.ups.spi.customstrade.command.QueryImportDeclarationRulesForListCommand;
import com.winit.ups.spi.customstrade.vo.ImportDeclarationRulesVo;

/**
 * 进口报关规则控制器
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * qiyongkang 	1.0  		2017年5月31日 	Created
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/ups/importDeclarationRules")
public class ImportDeclarationRulesController extends BaseController {

    @Resource
    private ImportDeclarationRulesService importDeclarationRulesService;

    @Resource
    private InboundOrderService           inboundOrderService;

    @Resource
    private GlobalTransferService         globalTransferService;

    @RequestMapping(value = "/queryByCondition")
    public String queryImportDeclarationRulesByCondition(@ModelAttribute RequestMsg requestMsg,
                                                         @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        // 校验
        ValidateUtil.isNotNull(json, "endWarehouseCode");
        ValidateUtil.isNotNull(json, "productCode");

        // 获取【目的仓】编码
        String endWarehouseCode = (String) json.get("endWarehouseCode");
        // 获取产品编码
        String productCode = (String) json.get("productCode");
        // 获取订单号
        String winitOrderNo = (String) json.get("winitOrderNo");

        InboundOrderImportDeclareRuleCommand command = new InboundOrderImportDeclareRuleCommand();
        command.setEndWarehouseCode(endWarehouseCode);
        command.setProductCode(productCode);
        command.setWinitOrderNo(winitOrderNo);
        List<ImportDeclarationRulesVo> list = inboundOrderService.queryImportDeclareRuleList(command);

        responseMsg.setData(list);
        return SUCCESS;
    }

    @RequestMapping(value = "/queryByFBACondition")
    public String queryImportDeclarationRulesByFBACondition(@ModelAttribute RequestMsg requestMsg,
                                                            @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 校验ID
        ValidateUtil.validNotNull(json, "id");
        ValidateUtil.validMaxLength(json, "id", 20, true);
        // 校验仓库编码
        ValidateUtil.validNotNull(json, "destWarehouseCode");
        ValidateUtil.validMaxLength(json, "destWarehouseCode", 60, true);
        // 获取订单ID
        Integer id = json.getInteger("id");
        String destWarehouseCode = json.getString("destWarehouseCode");

        GlobalTransferImportDeclareRuleCommand command = new GlobalTransferImportDeclareRuleCommand();
        command.setId(id);
        command.setDestWarehouseCode(destWarehouseCode);
        List<ImportDeclarationRulesVo> list = globalTransferService.queryImportDeclareRuleList(command);
        responseMsg.setData(list);
        return SUCCESS;
    }

    @RequestMapping(value = "/query")
    public String queryImportDeclarationRules(@ModelAttribute RequestMsg requestMsg,
                                              @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        // 获取国家编码
        String countryCode = (String) json.get("countryCode");
        // 获取报关方式
        String declarationWay = (String) json.get("declarationWay");
        // 获取进口报关价值类型，1还是2
        String importDeclarationValue = (String) json.get("importDeclarationValue");

        QueryImportDeclarationRulesForListCommand command = new QueryImportDeclarationRulesForListCommand();
        command.setIsActive("Y");
        command.setCountryCode(countryCode);
        command.setImportDeclarationValue(importDeclarationValue);
        command.setCustomsClearanceCode(declarationWay);

        List<ImportDeclarationRulesVo> list = importDeclarationRulesService.queryByParamForList(command);

        responseMsg.setData(list);
        return SUCCESS;
    }

    @RequestMapping(value = "/queryByCode")
    public String queryByCode(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        // 获取进口报关规则编码
        String code = (String) json.get("code");
        QueryImportDeclarationRulesByCodeCommand command = new QueryImportDeclarationRulesByCodeCommand();
        command.setCode(code);

        ImportDeclarationRulesVo importDeclarationRulesVo = importDeclarationRulesService.queryByCode(command);

        responseMsg.setData(importDeclarationRulesVo);
        return SUCCESS;
    }

}
